package Script;

import java.io.File;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * description: <br>
 * createTime: 2021/1/19 17:11 <br>
 *
 * @author zzh
 */
public class FileFinder {
    private static long DEFAULT_SIZE = 10 * 1024 * 1024;
    public static void findBig(String path, Consumer<File> consumer) {
        findBig(path, DEFAULT_SIZE, consumer);
    }

    public static void findBig(String path, long size, Consumer<File> consumer) {
        find(path, (f) -> f.length() > size, consumer);
    }

    public static void find(String path, Function<File, Boolean> func, Consumer<File> consumer) {
        recurseFind(new File(path), func, consumer);
    }


    private static void recurseFind(File file, Function<File, Boolean> func, Consumer<File> consumer) {
        if(!file.exists()) {
            return;
        }
        if(file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                recurseFind(listFile, func, consumer);
            }
        } else if(func.apply(file)) {
            consumer.accept(file);
        }
    }


    public static void main(String[] args) {
//        findBig("D:\\repository", 100_000_000);
        find("D:\\repository", (f) -> f.getName().endsWith(".war"), (f) -> System.out.println(f.getAbsolutePath() + "\t\t" + f.length()));
    }
}
